/**
 * 用户中心--发票--收寄地址管理
 */
import {Message} from "element-ui"
import {
    addrInfoList,
    deleteAddress,
    getCity,
    getDist,
    getProvince,
    saveAddrInfo,
    updateAddressStatus
} from "../../../../api"

export default {
    name: "transmit-addr",
    components: {},
    data() {
        let addressValid = (rule, value, callback) => {
            if (!this.addrInfo.province || !this.addrInfo.city || !this.addrInfo.district) {
                callback(new Error("请将地址信息补充完整"))
            } else {
                callback()
            }
        }
        let notNullValid = (rule, value, callback) => {
            if (!value || value == null || value == '') {
                callback(new Error("改项数据不能为空"))
            } else {
                callback()
            }
        }
        let postcodeValid = (rule, value, callback) => {
            if (!(/^[0-9]{6}$/.test(value))) {
                callback(new Error("请输入正确的邮政编码"))
            } else {
                callback()
            }
        }
        let phoneValid = (rule, value, callback) => {
            if (!(/^[1]([3-9])[0-9]{9}$/.test(value))) {
                callback(new Error("请输入正确的手机号码"))
            } else {
                callback()
            }
        }
        return {
            transmitAddrData: [],
            title: '新增寄送地址',
            deleteDialogVisible: false,//删除弹窗
            waitDeleteAddrId: null,//待删除的地址id
            showAreaPicker: false,//显示地址联动选择
            provinceList: [],
            cityList: [],
            distList: [],
            addrInfo: {},
            addrInfoRules: {
                detailAddress: [{required: true, validator: notNullValid, trigger: 'change'}],
                district: [{required: true, trigger: "change", validator: addressValid}],
                postcode: [{required: true, trigger: "change", validator: postcodeValid}],
                recipients: [{required: true, trigger: "blur", validator: notNullValid}],
                phone: [{required: true, trigger: "blur", validator: phoneValid}],
            }
        }

    },
    created() {
        this.initAddrInfo()
        this.searchAddrList()
        this.searchProvince()
    },
    methods: {
        //返回
        goBack() {
            //返回发票首页
            this.$emit('moduleIndexChange', 'home')
        },

        //初始化数据
        initAddrInfo() {
            this.addrInfo = {
                province: '',
                city: '',
                district: '',
                detailAddress: '',
                postcode: '',
                recipients: '',
                phone: '',
                status: false,
            }
        },

        //查询省
        searchProvince() {
            getProvince().then(res => {
                if (res.data.code === 200) {
                    this.provinceList = res.data.result
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("省份数据查询失败！")
            })
        },
        //查询市
        searchCity() {
            let param = {
                provinceId: this.addrInfo.province
            }
            getCity(param).then(res => {
                if (res.data.code === 200) {
                    this.cityList = res.data.result
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("市/区数据查询失败！")
            })
        },
        //查询县
        searchDist() {
            let param = {
                cityId: this.addrInfo.city
            }
            getDist(param).then(res => {
                if (res.data.code === 200) {
                    this.distList = res.data.result
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("县数据查询失败！")
            })
        },
        //显示删除弹窗
        showDeleteDialog(row) {
            this.deleteDialogVisible = true
            this.waitDeleteAddrId = row.id
        },
        //修改
        modification(row) {
            Message.warning("请修改信息后，点击保存！")
            this.title = '编辑收货地址'
            this.addrInfo = row
            this.addrInfo.detailAddress = row.detileAddress
        },
        //省市区change
        onChangeProvince() {
            this.addrInfo.city = ''
            this.addrInfo.district = ''
            this.searchCity()
        },
        onChangeCity() {
            this.addrInfo.district = ''
            this.searchDist()
        },
        //保存收寄地址
        saveAddrInfo() {
            //验证
            this.$refs['addrInfo'].validate((valid) => {
                if (valid) {
                    this.addrInfo.status = this.addrInfo.status ? 1 : 0
                    saveAddrInfo(this.addrInfo).then(res => {
                        if (res.data.code === 200) {
                            this.initAddrInfo()
                            this.searchAddrList()
                            this.$message({
                                message: '保存成功',
                                type: 'success'
                            })
                        } else {
                            this.$message({
                                message: res.data.msg,
                                type: 'warning'
                            })
                        }
                    }).catch(() => {
                        // this.$message.error("保存失败！")
                    })
                }
            })
        },
        //查询收货地址
        searchAddrList() {
            let param = {
                status: null
            }
            addrInfoList(param).then(res => {
                if (res.data.code === 200) {
                    this.transmitAddrData = res.data.result
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("获取收货地址失败！")
            })
        },
        //设为默认收货地址
        setDefault(row) {
            console.log(row)
            let param = {
                status: 1,
                id: row.id
            }
            updateAddressStatus(param).then(res => {
                if (res.data.code === 200) {
                    this.searchAddrList()
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    })
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("修改失败！")
            })
        },
        //删除收货地址
        deleteAddr() {
            let param = {
                id: this.waitDeleteAddrId
            }
            deleteAddress(param).then(res => {
                if (res.data.code === 200) {
                    this.searchAddrList()
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("修改失败！")
            })
        }


    }
}
